<?php
/**
 * fldcms
 * ============================================================================
 * 版权所有 2017 福建华晟网络科技，并保留所有权利。
 * 网站地址: http://www.fldcms.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: fld
 * Date: 2017-08-09
 */
namespace app\admin\controller;
use think\Request;
use think\helper\Time;
/**
 * 分类管理
 * @author fld
 *
 */
class CateController extends BaseController{
	function __construct(){
		parent::__construct();
		$this->db = model('Cate');
	}
	/**
	 * 分类列表
	 */
	public function indexOp(){
		$result = $this->getCondition();
		$list = $this->db->getCateList($result);
		$this->assign("list",$list);
		// 获取分页显示
		$page = $list->render();
		$this->assign('page', $page);
		return $this->fetch();
	}
	
	public function getCondition(){
		$result = array();
		$condition = array();
		$query = array();
		//关键字
		$keyword = input('param.keyword/s');
		if(!empty($keyword)){
			$condition['cate_name|cate_keyword'] = array('like','%'.$keyword.'%');
			$query['keyword'] = $keyword;
		}
		$this->assign('keyword',$keyword);
		$result['condition'] = $condition;
		$result['query'] = $query;
		return $result;
	}
	/**
	 * 添加分类信息
	 */
	public function addOp(){
	    if (chksubmit()){
	        $res = $this->db->addData();
	        if($res !== false){
	            $this->msg = "添加分类成功";
	            $result['error'] = 0;
	        }else{
	            $this->msg = "添加分类失败";
	            $result['error'] = 1;
	        }
	        $result['msg'] = $this->msg;
	        return json($result);
	    }else{
	        return $this->fetch();
	    }
	}
	/**
	 * 编辑分类信息
	 */
	public function editOp(){
		if (chksubmit()){
			$res = $this->db->updateData();
			if($res !== false){
				$this->msg = "编辑分类成功";
				$result['error'] = 0;
			}else{
				$this->msg = "编辑分类失败";
				$result['error'] = 1;
			}
			$result['msg'] = $this->msg;
			return json($result);
		}else{
			//分类信息
			$id = input('param.id');
			$cate_info = $this->db->getCateInfo(array('id'=>$id));
			$this->assign('vo',$cate_info);
			return $this->fetch();
		}
	}
	/**
	 * 删除分类信息
	 */
	public function deleteOp(){
		$res = $this->db->deleteData();
		if($res !== false){
			$result['error'] = 0;
		}else{
			$this->msg = "删除分类失败";
			$result['error'] = 1;
		}
		$result['msg'] = $this->msg;
		return json($result);
	}
	
	/**
	 * 检查标签名是否存在
	 */
	public function check_ajaxOp(){
		$cate_name = input('post.cate_name');
		$cate_info = $this->db->getCateInfo(array('cate_name'=>$cate_name));
		if($cate_info){
			return json(false);
		}else{
			return json(true);
		}
	}
	/**
	 * 检查标签名是否存在
	 */
	public function check_name_ajaxOp(){
		$cate_name = input('post.cate_name');
		$id = input('post.id');
		$condtion = array();
		$condtion['cate_name'] = $cate_name;
		$condtion['id'] = array('neq',$id);
		$cate_info = $this->db->getCateInfo($condtion);
		if($cate_info){
			return json(false);
		}else{
			return json(true);
		}
	}
	
}